﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using SmartProject.Data;

namespace SmartProject.Logging
{
    /// <summary>
    /// The logger class is responsible for logging all messages into the log table.
    /// </summary>
    public class Logger : Entity
    {
        private readonly int _severityLevelId;
        private readonly SeverityLevel _severityLevel = new SeverityLevel();

        /// <summary>
        /// Creates a new instance of Logger.
        /// </summary>
        public Logger()
        {
            _severityLevel.DataParameterName = "SeverityLevelId";
            _severityLevel.Parent = this;

            DataParameter[] dataParameters = new DataParameter[7];

            dataParameters[0] = new DataParameter("LogId", DataType.Integer);
            dataParameters[1] = new DataParameter("ClassName", 50);
            dataParameters[2] = new DataParameter("MethodName", 100);
            dataParameters[3] = new DataParameter("Description", 500);
            dataParameters[4] = new DataParameter("SeverityLevelId", DataType.Integer);
            dataParameters[5] = new DataParameter("UserId", DataType.Integer);
            dataParameters[6] = new DataParameter("Created", DataType.DateTime);

            base.Initialize(dataParameters);

            Preference preference = new Preference();
            preference.GetValue("LoggingLevelId");
            if (String.IsNullOrEmpty(preference.Value))
            {
                _severityLevelId =_severityLevel.GetId("Debug", false);
                preference.Save("LoggingLevelId", _severityLevelId.ToString());
            }
            else
            {
                _severityLevelId = Convert.ToInt32(preference.Value);
            }            
        }

        public static void Log(string className, string methodName, string description,
            string severityLevel)
        {
            Log(className, methodName, description, severityLevel, -1);
        }

        public static void Log(string className, string methodName, string description,
            string severityLevel, int userId)
        {
            Logger logger = new Logger();

            // Log only messages that are allowed.
            int compare = logger._severityLevel.GetId(severityLevel, false);
            if (compare >= logger._severityLevelId)
            {
                logger.SetAttribute("ClassName", className);
                logger.SetAttribute("MethodName", methodName);
                logger.SetAttribute("Description", description);
                logger.SetAttribute("SeverityLevelId", compare);
                logger.SetAttribute("UserId", userId);
                logger.Save();
            }
        }

        public override void Load()
        {
            // Do nothing.
        }

        public override void Save()
        {
            base.Run("smartproject_AddLog");
        }

        public override void Update()
        {
            // Do nothing.
        }

        public override void Remove()
        {
            // Do nothing.
        }
    }
}
